/**
 * @author David Young
 */
package csab.game.sprites;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;

import csab.core.Collidable;
import csab.core.ResourceManager;
import csab.core.Sprite;
import csab.game.DumbController;

/**
 * This class represents entities within the game whose
 * sole purpose is to generate random bits of speech.
 * @author David Young
 */
public class BotSprite extends Sprite {
	protected static final String DEFAULT_MESSAGE = "You cray-cray";
	private Image botr;
	private String collidername;
	private boolean drawMessage;
	private String message;
	private Font speechfont;
	private ResourceManager mgr;
	private int waitTime;
	
	public BotSprite() throws SlickException
	{
		 mgr = ResourceManager.getManager("resource/resources.xml");
		try {
			botr = mgr.getAnimation("player_fwd_idle").getImage(0);
			speechfont = mgr.getFont("speech");
		} catch (SlickException e) {
			Log.error(e.getMessage());
			throw e;
		}
		message = DEFAULT_MESSAGE;
		collidername = "";
		this.setController(new DumbController(this));
		this.setWidth(botr.getWidth());
		this.setHeight(botr.getHeight());
	}
	
	/* (non-Javadoc)
	 * @see csab.core.Entity#destroy()
	 */
	@Override
	protected void destroy() {
		mgr.freeAnimation("player_forward_walk");
		try {
			mgr.freeFont("speech");
		}
		catch(SlickException e)
		{
			throw new RuntimeException(e.getMessage());
		}
	}
	
	/* (non-Javadoc)
	 * @see csab.core.Sprite#doAction()
	 */
	@Override
	public void doAction(int delta) {
		super.doAction(delta);
		waitTime = Math.max(waitTime - delta, 0);
	}

	/* (non-Javadoc)
	 * @see csab.core.Collidable#doCollide(csab.core.Collidable)
	 */
	@Override
	public void doCollide(Collidable obj) {
		if(obj instanceof PlayerSprite)
		{
			waitTime = 2000;
			collidername = obj.getName();
			drawMessage = true;
		}
	}

	/* (non-Javadoc)
	 * @see csab.core.Entity#draw(int, int, org.newdawn.slick.Graphics)
	 */
	@Override
	public void draw(int x, int y, Graphics g) {
		if(drawMessage || waitTime > 0) {
			drawMessage = false;
			g.setColor(Color.black);
			g.setFont(speechfont);
			g.drawString(message + " " + collidername + "!", x - 10, y - 20);
		}
		g.drawImage(botr, x, y);
	}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#idle(int)
	 */
	@Override
	public void idle() {}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#jump(int)
	 */
	@Override
	public void jump() {}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#moveLeft(int)
	 */
	@Override
	public void moveLeft() {}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#moveRight(int)
	 */
	@Override
	public void moveRight() {}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#canAttack()
	 */
	@Override
	public boolean canAttack() {
		return false;
	}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#doAttack()
	 */
	@Override
	public void doAttack() {}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#prepareAttack()
	 */
	@Override
	public void prepareAttack() {}

	/* (non-Javadoc)
	 * @see csab.core.Sprite#readyToAttack()
	 */
	@Override
	public boolean readyToAttack() { return false; }

}
